package com.onpositive.wiki3.catmodels;

import java.util.Arrays;
import java.util.LinkedHashSet;

import com.onpositive.nlp.inflector.SimplePOFParser;
import com.onpositive.nlp.sequence.ClassifiedSequence;
import com.onpositive.nlp.sequence.ClassifiedSequence.Token;
import com.onpositive.wiki3.db.catmodel.Category;

public class DateBasedSpecialisationCategoryModel extends CoreBasedModel {

	@Override
	public int score(Category cat) {
		String[] tokens = cat.getTokens();
		if (!Character.isDigit(tokens[0].charAt(0))) {
			return 0;
		}
		if (tokens.length == 1) {
			return 0;
		}
		LinkedHashSet<String> s = new LinkedHashSet<String>(
				Arrays.asList(tokens));
		s.remove("bc");
		s.remove("millenium");
		boolean isSpec = false;
		boolean isNumber=false;
		int nc=0;
		for (String t : tokens) {
			if (SimplePOFParser.isAdjective(t)) {
				s.remove(t);
				isSpec = true;
			}
			try {
				String name = t;
				if (Character.isDigit(name.charAt(0))) {
					if (name.endsWith("s")) {
						name = name.substring(0, name.length() - 1);
					}
					if (name.endsWith("th")) {
						name = name.substring(0, name.length() - 2);
					}
					if (name.endsWith("th-century")) {
						name = name.substring(0,
								name.length() - "th-century".length());
					}
				}
				Integer.parseInt(name);
				s.remove(t);
				isNumber=true;
				nc++;
			} catch (Exception e) {

			}
		}
		if (isNumber&&  s.size() <= 2&&nc==1) {
			for (String m : s) {
				if (!SimplePOFParser.isNoun(m)) {
					return 0;
				}
			}
			cat.setTag(ICategoryModel.DATE_RELATED_CATEGORY);
			return 1;	
		}
		return 0;
	}

	@Override
	public String name() {
		return "simple specialisation";
	}

	@Override
	public boolean isPossibleCategorization(Category cat) {
		return true;
	}

}
